package org.freedom.fastdevpt.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.regex.Pattern;

public class GosDateUtils {

	static final DateTimeFormatter dateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
	static final DateTimeFormatter date = DateTimeFormatter.ofPattern("yyyy-MM-dd");
	static final DateTimeFormatter time = DateTimeFormatter.ofPattern("HH:mm:ss");
	/** 时间表达式 eg：12：22：30 */
	static final Pattern isTime = Pattern.compile("^(([01][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])$");

	/**
	 * 是否是时间格式eg: 23:30:30
	 * 
	 * @param time
	 * @return true or false
	 */
	public static boolean istime(String time) {
		if (null == time || time.length() == 0)
			return false;
		return isTime.matcher(time).matches();
	}

	/** 日期时间表达式 eg：2016-02-02 12：22：30 */
	static final Pattern isDT = Pattern.compile(
			"^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))\\s{1}(([01][0-9])|([2][0-3])):([0-5][0-9]):([0-5][0-9])$");

	/**
	 * 是否日期时间格式eg: 2016-12-12 23:30:30
	 * 
	 * @param dt
	 * @return true or false
	 */
	public static boolean isDatetime(String dt) {
		if (null == dt || dt.length() == 0)
			return false;
		return isDT.matcher(dt).matches();
	}

	/** 日期表达式 eg：2016-02-02 */
	static final Pattern isDate = Pattern.compile(
			"^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))$");

	/**
	 * 是否日期格式eg: 2016-12-12
	 * 
	 * @param dt
	 * @return true or false
	 */
	public static boolean isDate(String date) {
		if (null == date || date.length() == 0)
			return false;
		return isDate.matcher(date).matches();
	}

	public static Date from(LocalDateTime date) {

		return Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
	}

	public static LocalDateTime from(Date date) {

		return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
	}

	public static String formateDateTime(LocalDateTime ldt) {
		return ldt.format(dateTime);
	}

	public static String formateDate(LocalDateTime ldt) {
		return ldt.format(date);
	}
	public static String formatLongTime(long time){
	 return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).format(dateTime);
	}
	public static long now(){
		return Instant.now().getEpochSecond();
	}
}
